package com.zdft.promotion.common.util.rpc;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.AbstractFactoryBean;

import java.lang.reflect.Proxy;

/**
 * RPC客户端
 */
public class RpcClient extends AbstractFactoryBean<Object> {
    @Value("${esbHost}")
    private String ip;
    @Value("${esbPort}")
    private int    port;

    private final Class<?> clazz;

    public RpcClient(Class<?> clazz) {
        this.clazz = clazz;
    }

    @Override
    public Class<?> getObjectType() {
        return clazz;
    }

    @Override
    protected Object createInstance() throws Exception {
        Class<?>[] inters = {clazz};
        RpcHandler handler = new RpcHandler(clazz, ip, port);
        return Proxy.newProxyInstance(getClass().getClassLoader(), inters, handler);
    }
}
